package zclCluster;

import wikiextract.util.io.*;
import java.io.*;
import java.util.*;
import java.util.*;
import java.util.Map.*;

class ExtVal{
	String ext;
	double val;
	public ExtVal(String ext, double val){
		this.ext = ext;
		this.val = val;
	}
}

class AidExtVal{

	
	int aid;
	List<ExtVal> extvals = new ArrayList<ExtVal>();
	public AidExtVal(int id){
		this.aid = id;
	}
	public void add(String ext, double val){
		this.extvals.add(new ExtVal(ext,val));
	}
	private void sortByVal(){
		Collections.sort(extvals,new Comparator<ExtVal>(){
			@Override
			public int compare(ExtVal o1, ExtVal o2) {
				// TODO Auto-generated method stub
				return o2.val-o1.val>0?1:-1;
			}
		});
	}
	public ExtVal return_self_f1(){
		ExtVal res = null;
		for(int i=0;i<extvals.size();i++){
			if(extvals.get(i).ext.equals(""+this.aid)){
				res = extvals.get(i);
				break;
			}
		}
		return res;
	}
	
	public ExtVal return_merge_f1(){
		ExtVal res = null;
		sortByVal();
		for(int i=0;i<extvals.size();i++){
			if(extvals.get(i).ext.contains("-")){
				res = extvals.get(i);
				break;
			}
		}
		return res;
		
	}
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append(aid).append("\t");
		for(ExtVal ev: extvals){
			sb.append(ev.ext+":"+ev.val+" ");
		}
		return sb.toString();
	}

}
public class BinaryBaggingImp {

	/**
	 * @param args
	 */
	static HashMap<Integer,AidExtVal>aidextvalmap = new HashMap<Integer,AidExtVal>();
	
	private static void addAidExtVal(int aid, String ext, double f1){
		AidExtVal aev = aidextvalmap.get(aid);
		if(aev == null){
			aev = new AidExtVal(aid);
			aev.add(ext, f1);
			aidextvalmap.put(aid, aev);
		}else{
			aev.add(ext, f1);
		}
	}
	
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String dir = args[0];
		String nearfile = dir+"/top10near_att";
		String dir_binaryresult = args[0]+"/binaryBaggingImprove";
		
		File f = new File(dir_binaryresult);
		String []files = f.list();
		for(int i=0;i<files.length;i++){
			DelimitedReader dr = new DelimitedReader(dir_binaryresult+"/"+files[i]);
			String []line;
			while((line = dr.read())!=null){
				String train_id_list = line[0];
				int test_id = Integer.parseInt(line[1]);
				double f1 = Double.parseDouble(line[3]);
				addAidExtVal(test_id,train_id_list,f1);
			}
			dr.close();
		}
		
		DelimitedReader dr = new DelimitedReader(nearfile);
		String []line;
		while((line = dr.read())!=null){
			int test_id = Integer.parseInt(line[0]);
			if(!aidextvalmap.containsKey(test_id))continue;
			String []split = line[1].split(" |:");
			for(int i=0;i<split.length;i+=2){
				if(split[i].equals(""+test_id)){
					addAidExtVal(test_id,test_id+"",Double.parseDouble(split[i+1]));
					break;
				}
			}
		}
		Iterator<Entry<Integer,AidExtVal>>it =  aidextvalmap.entrySet().iterator();
		while(it.hasNext()){
			Entry<Integer,AidExtVal>e = it.next();
			int aid = e.getKey();
			AidExtVal aev = e.getValue();
			ExtVal self = aev.return_self_f1();
			ExtVal merge = aev.return_merge_f1();
			if(self != null && merge != null){
				System.out.println(aid+"\t"+self.val+"\t"+merge.val);
			}
		}
	}
}
